<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.*" %>
<%@ page import="au.com.somethingwritten.model.*" %>
	<%
           if (request.getParameter("fromMasterPage") == null)
           {
                %>
                  <jsp:forward page="masterPage.jsp">
                    <jsp:param name="embeddedPageName" value 
                     ="cartPage.jsp"/>
                     <jsp:param name="submitPageName" value 
                     ="cartPage.jsp"/>
                  </jsp:forward>
                <%
           }
      %>
      	<%
  			ShoppingCart cart = (ShoppingCart)session.getAttribute("cart");
			if(cart == null){
				cart = new ShoppingCart();
			}
		%>
      	<h2>Your Shopping Cart</h2>
      	<div class="result_holder">
	      	<div class="cart_header_footer">
	      		<form name="go_to_checkout" method="get" action="/SomethingWritten/pay">
					<button class="checkout_btn button" type="submit" <%if(cart.getTotalItems() < 1){out.print("disabled=\"disabled\"");} %>>checkout</button>
					
				</form>
	      		<strong>Sub-Total:</strong> $<% out.print(String.format("%1$,.2f", cart.getCost())); %>
	      	</div>
		<%
			if(cart.size() < 1)
				out.print("<h3>No Items in cart</h3>\n");
  			for(int i = 0; i < cart.size(); i++){
    			out.print("<div class=\"result_book\"> \n");
    			//create a link for the image.
    			out.print("<a href=\"/SomethingWritten/bookDetails?book=" + cart.get(i).getItem().getId().toString() + "\">\n");
    			//display image and set alt text 
    			out.print("<img src=\"" + cart.get(i).getItem().getImageFile() + "\" alt=\"" + cart.get(i).getItem().getTitle() + " Cover\" /> \n" );
    			//end link
    			out.print("</a>\n");
    			out.print("<div class=\"book_detail\"> \n");
    			//display title
    			out.print("<h3>" + cart.get(i).getItem().getTitle() + "</h3>");
    			//display author
    			out.print("by " + cart.get(i).getItem().getAuthor() + "<br />");
    			//display price
    			out.print("Price: $" + String.format("%1$,.2f", cart.get(i).getCost()) + "<br /><br />");
    			//display error for low stock
    			if(cart.get(i).getItem().getQuantity() < (cart.get(i).getQuantity() + 5)){
    				out.print("<div class=\"stock_err\">Warning: This book's stock is low, delivery may be delayed.</div>");
    			}
    			//display quantity edit form
    			out.print("<form name=\"qty_update_form\" method=\"post\" action=\"updateQty\">");
    			//display input with current quantity
    			out.print("<label class=\"qty_lbl\" for=\"quantity\">qty: </label>");
    			out.print("<input autocomplete=\"off\" class=\"qty_input\" type=\"text\" name=\"quantity\" title=\"book quantity\" value=\"" + cart.get(i).getQuantity().toString() + "\"/>");
    			//store a hidden copy of the book's id.
    			out.print("<input type=\"hidden\" name=\"book_id\" value=\"" + cart.get(i).getItem().getId().toString() + "\">");
    			//display button to update book quantity
    			out.print("<button class=\"qty_btn\" type=\"submit\" name=\"qty_btn\">update</button>");
    			out.print("</form>");
    			out.print("</div> \n");
    			out.print("<div class=\"float_clear\"></div> \n");
    			out.print("</div> \n");
  			}
		%>
			<div class="cart_header_footer">
	      		<form name="go_to_checkout" method="get" action="/SomethingWritten/pay">
					<button class="checkout_btn button" type="submit" <%if(cart.getTotalItems() < 1){out.print("disabled=\"disabled\"");} %>>checkout</button>
					
				</form>
	      		<strong>Sub-Total:</strong> $<% out.print(String.format("%1$,.2f", cart.getCost())); %>
	      	</div>
		</div>